<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>预收款管理</title>
<link href="${basePath }/css/ui.css" rel="stylesheet" type="text/css">
<jsp:include page="/header.jsp"></jsp:include>
<script type="text/javascript">
	$(function() {
		initDialog();
	});
	
	function resetCash(input) {
		var sum_total = Number($("#sum_total").text());
		var bank_amount = Number($("[name='bank_amount']").val());
		var vip_amount = Number($("[name='vip_amount']").val());
		var wipe_amount = Number($("[name='wipe_amount']").val());
		$("[name='cash_amount']").val(sum_total.sub(bank_amount).sub(vip_amount).sub(wipe_amount));
	}
	
	//使用会员卡
	function useVipCard(check) {
		var next_span = $(check).parent().parent().next().find("span");
		if(check.checked) {
			next_span.show();
		} else {
			next_span.find("input").val("");
			next_span.hide();
		}
	}
	
	function confirm() {
		//销售总金额 - 抹零金额 = 现金支付 + 银行刷卡 + 会员刷卡
		var sum_total = $("#sum_total").text();
		var cash_amount = $("[name='cash_amount']").val();
		var bank_amount = $("[name='bank_amount']").val();
		var serial_no = $("[name='serial_no']").val();
		var vip_amount = $("[name='vip_amount']").val();
		var vip_no = $("[name='vip_no']").val();
		var wipe_amount = $("[name='wipe_amount']").val();
		if(bank_amount!=""&&serial_no=="") $.jqalert("请输入银行支付流水号！！", "");
		else if(vip_amount!=""&&vip_no=="") $.jqalert("请输入会员卡号！！", "");
		else if(Number(sum_total)!=Number(cash_amount)+Number(bank_amount)+Number(vip_amount)+Number(wipe_amount)) $.jqalert("金额输入有误！！", "");
		else {
			var inputs = $(".b_div").find("input[type='text']");
			var pay_table = $("#confirm_pay_table");
			$.each(inputs, function(i, n){
				var name = $(n).attr("name");
				var value = $(n).val();
				if(name!="serial_no"&&name!="vip_no") value = Number(value);
				pay_table.find("[name='"+name+"']").text(value);
			});
			$("#vip_input_div").find("[name='vip_no']").text(vip_no);
			pay_table.find("[name='org_amount']").text(sum_total);
			pay_table.find("[name='now_amount']").text(Number(sum_total)-Number(wipe_amount));
			if(Number(vip_amount)==0) $("#vip_input_div").hide();
			else $("#vip_input_div").show();
			$("#confirm_pay_dialog").dialog("open");
		}
	}
	
	function initDialog() {
		$("#confirm_pay_dialog").dialog({
			title:"消费确认",
			autoOpen: false,
			height: 400,
			width: 300,
			modal: true,
			buttons:[{
				text:"确定",
				click:function() {
					var payType = $("input[name='pay_type']:checked").val();
					$.ajax({
						url:"${basePath}/advance/buy.htm",
						type:"post",
						data:$("#temp_form").serialize()+"&vip_pwd="+$("#confirm_pay_table").find("input[name='vip_pwd']").val(),
						dataType:"json"
					}).done(function(data){
						$.jqalert(data.msg, "提示", function() {
							if(data.isSuccess) {
								$("#confirm_pay_dialog").dialog("close");
								window.location="${basePath}/buyback/index.htm";
							}
						});
					});
				}
			},{
				text:"取消",
				click:function() {
					$("#confirm_pay_dialog").dialog("close");
				}
			}]
		});
	}
	
	function reset() {
		$("#pay_first_div").find("input").val("");
		$("#pay_first_div").find("input").first().val($("#sum_total").text());
	}
</script>
</head>
<body>
	<div class="main">
        <div class="cont">
            <h2 class="title">结算的商品：</h2>
            <div class="b_div">
				<p class="clearfix">
					<span class="info_s fl">预收款总金额：<span id="sum_total">${price.real_price }</span>元</span>
				</p>
			</div>
			
			<h2 class="title"></h2>
            <div class="b_div" id="pay_first_div">
            <form id="temp_form">
				<p class="clearfix">
					<span class="info_s fl">现金支付：<input type="text" name="cash_amount" value="${price.real_price }">元</span>
				</p>
				<p class="clearfix">
					<span class="info_s fl">银行刷卡：<input type="text" name="bank_amount" onblur="resetCash(this);">元</span>
					<span class="info_s fl">流水号：<input type="text" name="serial_no"></span>
				</p>
				<p class="clearfix">
					<span class="info_s fl">会员刷卡：<input type="text" name="vip_amount" onblur="resetCash(this);">元</span>
				</p>
				<p class="clearfix">
					<span class="info_s fl">抹零：<input type="text" name="wipe_amount" onblur="resetCash(this);">元</span>
				</p>
				<p class="clearfix">
					<span class="info_s fl">是否刷卡：<input type="checkbox" onclick="useVipCard(this);">会员卡</span>
				</p>
				<p class="clearfix">
					<span class="info_s fl" style="display:none;">卡号：<input name="vip_no" type="text">会员卡<br></span>
				</p>
				</form>
			</div>
			<div class="text_c">
	        	<a href="javascript:;" class="yellow_btn btn" onclick="confirm();">确定</a>
	        	&nbsp;&nbsp;
	        	<a href="javascript:;" class="yellow_btn btn" onclick="reset();">重置</a>
	        	<!-- &nbsp;&nbsp;
	        	<a href="javascript:;" class="yellow_btn btn" onclick="test();">测试</a> -->
	        </div>
		</div>
	</div>
	
	<div id="confirm_pay_dialog">
		<table id="confirm_pay_table">
			<!-- <tr><th>总金额：</th><td><span></span>元</td></tr> -->
			<tr><th>现金支付：</th><td><span name="cash_amount"></span>元</td></tr>
			<tr><th>银行刷卡：</th><td><span name="bank_amount"></span>元</td><th>流水号：</th><td><span name="serial_no"></span></td></tr>
			<tr><th>会员刷卡：</th><td><span name="vip_amount"></span>元</td></tr>
			<tr><th>抹零：</th><td><span name="wipe_amount"></span>元</td></tr>
			<tr><th>应收金额：</th><td><span name="org_amount"></span>元</td></tr>
			<tr><th>实收金额：</th><td><span name="now_amount"></span>元</td></tr>
		</table>
		<div id="vip_input_div">
		<form>
		<table>
			<tr><th>卡号：</th><td><span name="vip_no"></span></td></tr>
			<tr><th>请输入密码：</th><td><input type="password" name="vip_pwd"></td></tr>
		</table>
		</form>
		</div>
	</div>
</body>
</html>